import { Component, OnInit, Input } from '@angular/core';
import { ReportService } from '../../../report.service';
import { LazyLoadEvent } from 'primeng/api';
import { DialogService } from 'primeng/primeng';
import { ReportStandardDetailComponent } from './report-standard-detail.component';

@Component({
  selector: 'report-statis-list',
  templateUrl: './report-statis-list.component.html',
  styleUrls: ['./report-statis-list.component.scss']
})
export class ReportStatisListComponent implements OnInit {

  @Input()
  set id(reportId: number) {
    if (!reportId) return;
    //加载表头
    this.conds = [];
    this.records = [];
    this.reportId = reportId;
    //加载统计信息
    this.reportService.loadReport(reportId).subscribe(report => {
      this.report = report;
      //加载统计表头
      this.reportService.loadStatisHeaderColumns(reportId).subscribe(data => {
        // 移除冻结列
        this.frozenHeader = data.headers[0][0];
        let c = data.headers[0].slice(1, data.headers[0].length)
        data.headers[0] = c;
        this.headers = data.headers;
        //移除冻结列
        this.cols = data.cols;
        this.frozenCols = [this.cols[0]]
        this.scrollableCols = this.cols.slice(1, this.cols.length);
        this.loadStatisRecords(null);
      });
    });
  }
  get id(): number { return this.reportId; }

  report: any = {};
  reportId: number = 0;
  loading: boolean = false;
  records: any[] = []; //数据记录
  headers: any[] = []; //表头
  frozenHeader: any[] = [];
  cols: any[] = []; //列信息
  scrollableCols: any[] = [];
  frozenCols: any[] = [];

  conds: any[] = [];
  constructor(
    private dialogService: DialogService,
    private reportService: ReportService) { }

  ngOnInit() {
    this.reportService.routeMsgPush$.subscribe(conds => {
      this.conds = conds;
      this.loadStatisRecords(this.conds);
    });
  }

  /**查询数据 */
  loadStatisRecords(conds) {
    this.loading = true;
    if (conds) {
      this.conds = conds;
    }
    this.reportService.loadStatisRecords(this.conds, this.reportId).subscribe(data => {
      this.loading = false;
      this.records = data;
    });
  }


  //统计类报表数据反查
  showDetail(rowData, fieldName) {
    //计算反查条件
    let params = [];
    this.conds.forEach(e => params.push(e))
    //分组条件
    debugger
    let groups = this.frozenCols[0]['groupFieldName'].substr(1).split('&');
    let grpValues = rowData['group_value'].substr(1).split('&');
    for (let i = 0; i < grpValues.length; i++) {
      params.push({
        comparetor: "=",
        dataTypeId: 1,
        fieldName: groups[i],
        value: grpValues[i],
        value1: ""
      })
    }
    this.cols.forEach(e => {
      // 统计条件
      if (e.fieldName == fieldName) {
        if (e.subType == 'sum') {
          params.push({
            comparetor: ">",
            dataTypeId: e.dataTypeId,
            fieldName: e.fieldName,
            value: "0",
            value1: ""
          })
        } else if ('max' == e.subType || 'min' == e.subType) {
          params.push({
            comparetor: "=",
            dataTypeId: e.dataTypeId,
            fieldName: e.fieldName,
            value: rowData[e.fieldName],
            value1: ""
          })
        }
      }
    });
    //打开反查详情界面
    const ref = this.dialogService.open(ReportStandardDetailComponent, {
      'header': '详情数据',
      'width': '80%',
      contentStyle: { "height": "80vh" },
      data: { 'reportId': this.reportId, conds: params },
    });
  }

  // 文件下载
  download() {
    this.reportService.downloadReport(this.reportId, this.conds, this.report.reportName + '.xlsx').subscribe();
  }
}
